<!-- 修改头像 -->
<script setup>
import { ref } from 'vue'
import { Plus, Upload } from '@element-plus/icons-vue'
// import { useUserStore } from '@/stores'
// import { userUpdateAvatarService } from '@/api/user'

// const imgUrl = ref(userStore.user.user_pic)
const uploadRef = ref()
const onSelectFile = (uploadFile) => {
  // 基于 FileReader 读取图片做预览
  const reader = new FileReader()
  reader.readAsDataURL(uploadFile.raw)
  reader.onload = () => {
    imgUrl.value = reader.result
  }
}
</script>

<template>
  <div class="my-avatar">
    <el-card style="opacity: 0.96; width: 50rem; height: 28.25rem; margin-top: 3.125rem">
      <el-upload
        ref="uploadRef"
        :auto-upload="false"
        class="avatar-uploader"
        :show-file-list="false"
        :on-change="onSelectFile"
      >
        <img src="@/assets/head1.jpeg" class="avatar" />
        <el-icon class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>

      <br />

      <el-button type="primary" :icon="Plus" size="large">选择图片</el-button>
      <el-button type="success" :icon="Upload" size="large">上传头像</el-button>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.my-avatar {
  // background: url('@/assets/images/学习.jpg') no-repeat 60% center / 245px auto;
  margin: 0px;
  background: url(@/assets/images/avatar.jpg) no-repeat;
  background-size: 100% 100%;

  width: 100%;
  height: 100%;
  position: absolute;
  // z-index: -1;
  display: flex;
  justify-content: center;
  // align-items: center;
  .el-button {
    margin: 0px 20px;
  }
}
.avatar-uploader {
  :deep() {
    .avatar {
      width: 278px;
      height: 278px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 278px;
      height: 278px;
      text-align: center;
    }
  }
}
</style>
